import {request} from "@/utils/request.js";
import { createPinia } from 'pinia';
import { useTokenStore } from '@/store/modules/auth';

// const pinia = createPinia();

export async function login(data) {
    // const tokenStore = useTokenStore(pinia);
    const tokenStore = useTokenStore();
    try {
      const response = await request({
        method: 'POST',
        url: '/user/login',
        data,
        headers: {
          'Content-Type': 'application/json'
        }
      });
      if (response.data.token) {
        const token = response.data.token;
        tokenStore.setToken(token);
        return response;
      } else {
        throw new Error("Token not found in response");
      }
    } catch (error) {
      console.error("Login failed:", error);
      throw error;
    }
  }

export async function getcaptcha(email){
    return request({
        method: 'GET',
        url: `/user/captcha?email=${encodeURIComponent(email)}`,
    })
}

export async function register(data){
    return request({
        method: 'POST',
        url: '/user/register',
        data,
    })
}
export function resetpassword_getcaptcha(email,username){
    return request({
        method: 'GET',
        url: '/user/reset_password',
    })
}
export async function resetpassword(data){
    return request({
        method: 'POST',
        url:'/user/reset_password',
        data,
    })
}